package app;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.MotionEvent;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.apache.log4j.Logger;
import java.util.HashMap;
import util.BReceiverUtil.HomeWatcherBR;
import util.HomeUtil;
import util.LogUtil;

public class BaseActivity extends FragmentActivity implements HomeWatcherBR.OnHomePressedListener{
  public static ImageLoader mBaseImageLoader;
  public static HashMap<String,Object> mMap; //访问网络
  private HomeWatcherBR mHomeWatcherBR;
  private boolean mJuderHome = false;   //判断是否按Home键
  private Logger mLogger;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LogUtil.eE("","      onCreate 进来             ");
    //PushAgent.getInstance(getApplicationContext()).onAppStart();//统计应用启动数据
    if(mBaseImageLoader == null){mBaseImageLoader = MyApplication.getApp().getImageLoader();}

    mLogger = Logger.getLogger(BaseActivity.class);

  }

  @Override protected void onResume() {
    super.onResume();

    mHomeWatcherBR = new HomeWatcherBR(this);
    mHomeWatcherBR.setOnHomePressedListener(this);
    if(mJuderHome == true){
      mLogger.info("      按home键 后恢复             ");
      automaticEntry();
      mJuderHome = false;
    }
    //注：回调 1
  }

  @Override protected void onPause() {
    super.onPause();
    if(mHomeWatcherBR !=null){
      mHomeWatcherBR.setOnHomePressedListener(null);
      mHomeWatcherBR.stopWatch();
    }
  }

  @Override protected void onStart() {
    super.onStart();
  }

  @Override protected void onStop() {
    super.onStop();
  }

  @Override protected void onDestroy() {
    super.onDestroy();

    if(mMap !=null){mMap.clear();}
  }

  /**
   * 吐司
   */
  public void getToast(String string){Toast.makeText(this,string,Toast.LENGTH_SHORT).show();}

  @Override public boolean dispatchTouchEvent(MotionEvent event) {
    //注：回调 3
    return super.dispatchTouchEvent(event);
  }

  @Override public void onHomePressed() {
    mJuderHome = true;
  }

  @Override public void onHomeLongPressed() {

  }

  /**
   *放回一个Logger 对象
   */
  public Logger getLogger(){
    return mLogger;
  }

  /**
   * 当按home键退出后再连接一次网络，自动登录
   */
  private void automaticEntry() {
    HomeUtil.getHemeUtilNew().setWiFiView(this);
  }
}
